package com.example.a86186.apps.util;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public  final class SocketLocationListener implements LocationListener {

    private WebSocketClient client;

    public SocketLocationListener(WebSocketClient client) {
        this.client = client;
    }

    @Override
    public void onLocationChanged(Location location) {
        String lo = location.getAltitude() + "  " + location.getLatitude();
        System.out.println("lo            "+lo);
        if (client.isOpen()) {
            client.send(lo);
        }

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        String lo = provider + "  " + status;

        client.send(lo);
    }

    @Override
    public void onProviderEnabled(String provider) {
        client.send(provider);
    }

    @Override
    public void onProviderDisabled(String provider) {
        client.send(provider);
    }






    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        URI uri = new URI("http://192.168.56.102:8080/originalendpoint");
        WebSocketClient client= new WebSocketClient(uri) {
            @Override
            public void onOpen(ServerHandshake handshakedata) {
                System.out.println("onopen");

                LocationMessage locationMessage = new LocationMessage();
                locationMessage.setAlt("alt");
                locationMessage.setLat("lat");
                send(locationMessage.toString());

            }

            @Override
            public void onMessage(String message) {
                System.out.println("onMessage  " + message);
//                send("222");
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                System.out.println(String.format("onclose  %d  %s   %s", code, reason, String.valueOf(remote)));
            }

            @Override
            public void onError(Exception ex) {
                System.out.println("onError  " + ex.getMessage());
            }
        };
        client.connect();
        Thread.sleep(10000);
        client.close();

    }
}
